package com.flyco.modules.oms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.OmsOrderProcessMemo;
import com.flyco.modules.oms.model.OmsOrderProcessMemoModel;
import com.flyco.modules.oms.service.IOmsOrderProcessMemoService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.oms.service.IOmsOrderService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 客服处理跟进
 * @Author: flyco
 * @Date: 2020-03-17
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "客服处理跟进")
@RestController
@RequestMapping("/f/oms/omsOrderProcessMemo")
public class OmsOrderProcessMemoController extends BaseController<OmsOrderProcessMemo, IOmsOrderProcessMemoService> {
    @Autowired
    private IOmsOrderProcessMemoService omsOrderProcessMemoService;
    @Autowired
    private IOmsOrderService omsOrderService;

    @AutoLog(value = "客服处理跟进-根据订单号查询")
    @ApiOperation(value = "客服处理跟进-根据订单号查询", notes = "客服处理跟进-根据订单号查询")
    @GetMapping(value = "/listByOrderId")
    public Result<?> listByOrderId(@RequestParam(name = "id") String id) {
        Result<List<OmsOrderProcessMemo>> result = new Result<>();
        LambdaQueryWrapper<OmsOrderProcessMemo> query = new LambdaQueryWrapper<>();
        query.eq(OmsOrderProcessMemo::getOrderId, id);
        query.orderByDesc(OmsOrderProcessMemo::getCreateTime);
        List<OmsOrderProcessMemo> list = omsOrderProcessMemoService.list(query);
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }

    /**
     * 添加
     *
     * @param omsOrderProcessMemoModel
     * @return
     */
    @AutoLog(value = "客服处理跟进-添加")
    @ApiOperation(value = "客服处理跟进-添加", notes = "客服处理跟进-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody OmsOrderProcessMemoModel omsOrderProcessMemoModel) {
        OmsOrderProcessMemo omsOrderProcessMemo = BeanUtils.instantiateClass(OmsOrderProcessMemo.class);
        BeanUtils.copyProperties(omsOrderProcessMemoModel, omsOrderProcessMemo);
        if (!oConvertUtils.isEmpty(omsOrderProcessMemo.getContent())) {
            omsOrderProcessMemoService.save(omsOrderProcessMemo);
        }

        OmsOrder order = new OmsOrder();
        order.setId(omsOrderProcessMemoModel.getOrderId());
        order.setProcessStatus(omsOrderProcessMemoModel.getProcessStatus());
        order.setProcessTag(omsOrderProcessMemoModel.getProcessTag());
        order.setProcessRemindTime(omsOrderProcessMemoModel.getProcessRemindTime());
        omsOrderService.updateById(order);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param omsOrderProcessMemo
     * @return
     */
    @AutoLog(value = "客服处理跟进-编辑")
    @ApiOperation(value = "客服处理跟进-编辑", notes = "客服处理跟进-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody OmsOrderProcessMemo omsOrderProcessMemo) {
        omsOrderProcessMemoService.updateById(omsOrderProcessMemo);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "客服处理跟进-通过id删除")
    @ApiOperation(value = "客服处理跟进-通过id删除", notes = "客服处理跟进-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        omsOrderProcessMemoService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "客服处理跟进-批量删除")
    @ApiOperation(value = "客服处理跟进-批量删除", notes = "客服处理跟进-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.omsOrderProcessMemoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "客服处理跟进-通过id查询")
    @ApiOperation(value = "客服处理跟进-通过id查询", notes = "客服处理跟进-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        OmsOrderProcessMemo omsOrderProcessMemo = omsOrderProcessMemoService.getById(id);
        return Result.ok(omsOrderProcessMemo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param omsOrderProcessMemo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OmsOrderProcessMemo omsOrderProcessMemo) {
        return super.exportXls(request, omsOrderProcessMemo, OmsOrderProcessMemo.class, "客服处理跟进");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OmsOrderProcessMemo.class);
    }

}
